package guij.controller;

import guij.dao.mapper.ItemMapper;
import guij.taobao.TaobaokeInterface;
import guij.taobao.TopInterface;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.taobao.api.model.Item;

public class GetOnSellItems implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {

		int num;
		String sessionkey = arg0.getSession().getAttribute("top_session").toString();
		String cid = arg0.getParameter("cid");
		String strnum = arg0.getParameter("num");
		if(strnum==null){
			num = 30;
		}else{
			num = Integer.getInteger(strnum);
		}
		List<Item> items = topInterface.getOnSaleItems(sessionkey, num, cid);

		Map<String, List<Item>> ret = new HashMap<String, List<Item>>();
		ret.put("items", items);
		return new ModelAndView(getViewPage(),ret);
	}

	public void setTopInterface(TopInterface topInterface) {
		this.topInterface = topInterface;
	}

	public TopInterface getTopInterface() {
		return topInterface;
	}

	/**
	 * @param taobaokeInterface
	 *            the taobaokeInterface to set
	 */
	public void setTaobaokeInterface(TaobaokeInterface taobaokeInterface) {
		this.taobaokeInterface = taobaokeInterface;
	}

	/**
	 * @return the taobaokeInterface
	 */
	public TaobaokeInterface getTaobaokeInterface() {
		return taobaokeInterface;
	}

	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}

	public String getViewPage() {
		return viewPage;
	}

	public void setItemMapper(ItemMapper itemMapper) {
		this.itemMapper = itemMapper;
	}

	public ItemMapper getItemMapper() {
		return itemMapper;
	}

	private TaobaokeInterface taobaokeInterface;

	private TopInterface topInterface;
	private ItemMapper itemMapper;

	private String viewPage;
}
